import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../components/app_conifg.dart';
import '../../../entity/lottery/lottery_list_bean.dart';
import '../../../utils/AppData.dart';
import '../../../utils/ThemeConfig.dart';
import '../../../utils/Tools.dart';

class LotteryResultListWidget extends StatefulWidget {
  final LotteryListBean? lotteryListBean;

  LotteryResultListWidget({super.key, required this.lotteryListBean});

  @override
  _LotteryResultListWidgetState createState() =>
      _LotteryResultListWidgetState();
}

class _LotteryResultListWidgetState extends State<LotteryResultListWidget>
    with TickerProviderStateMixin {
  TabController? tabCtrLottery;
  bool isAll = true;

  @override
  void initState() {
    super.initState();
    tabCtrLottery = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.lotteryListBean != null)
      print('---->> 123 ${widget.lotteryListBean?.my?.length}');
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: EdgeInsets.all(6),
          width: MediaQuery.of(Get.context!).size.width,
          height: 190,
          decoration: BoxDecoration(
            color: Tools.fromHex('#FFE8CE'), // 背景颜色
            borderRadius: BorderRadius.circular(8), // 设置圆角
          ),
          child: Card(
            elevation: 0,
            color: Colors.transparent,
            //shape 设置边，可以设置圆角
            shape: RoundedRectangleBorder(
              side: new BorderSide(width: 1, color: Tools.fromHex('#FA6B2F')),
              borderRadius: BorderRadius.all(Radius.circular(8.0)),
            ),
            child: Column(
              children: [
                Container(
                  child: TabBar(
                      dividerHeight: 0,
                      controller: tabCtrLottery,
                      indicator: UnderlineTabIndicator(
                        borderSide: BorderSide(
                          width: 2.0, // 下划线的高度
                          color: Color(0xffea8334),
                        ),
                        insets: EdgeInsets.symmetric(
                            horizontal: 60.0, vertical: 10), // 文本与下划线间的距离
                      ),
                      labelColor: Color(0xffea8334),
                      labelPadding: EdgeInsets.zero,
                      tabs: const [
                        Tab(text: '中奖名单'),
                        Tab(text: '我的奖品'),
                      ]),
                  padding: EdgeInsets.symmetric(horizontal: 40),
                ),
                Expanded(
                  child: TabBarView(
                    controller: tabCtrLottery,
                    children: [
                      Container(
                          padding: EdgeInsets.only(left: 8.w, right: 8.w),
                          child: ListView.builder(
                            itemCount: widget.lotteryListBean?.all?.length ?? 0,
                            itemBuilder: (context, index) {
                              return lotteryResultItem(
                                widget.lotteryListBean!.all![index],
                              );
                            },
                          )),
                      Container(
                          padding: EdgeInsets.only(left: 8.w, right: 8.w),
                          child: ListView.builder(
                            itemCount: widget.lotteryListBean?.my?.length ?? 0,
                            itemBuilder: (context, index) {
                              return lotteryResultMyItem(
                                widget.lotteryListBean!.my![index],
                              );
                            },
                          )),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
        SizedBox(
          height: 18,
        ),
        GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Image.asset(
            "assets/images/close.png",
            color: Colors.white,
          ),
        )
      ],
    );
  }

  Widget lotteryResultItem(All bean) {
    return Container(
        height: 40,
        child: Row(
          children: [
            Expanded(
                child: Row(
              // crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                BaseImage(
                  url: "${bean.avatar}",
                  width: 20,
                  height: 20,
                  borderRadius: BorderRadius.circular(30),
                ),
                SizedBox(
                  width: 10,
                ),
                Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        '${bean.nickName}',
                        style:
                            TextStyle(fontSize: 10, color: Color(0xff333333)),
                      ),
                      Text(
                        '${bean.drawDate}',
                        style: TextStyle(fontSize: 8, color: Color(0xff666666)),
                      )
                    ])
              ],
            )),
            Expanded(
              child: Text.rich(
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  TextSpan(
                    text: '获得',
                    children: [
                      TextSpan(
                        text: '${bean.goodsName}',
                        style:
                            TextStyle(fontSize: 10, color: Color(0xffff5141)),
                      ),
                      TextSpan(
                        text: '一份',
                        style:
                            TextStyle(fontSize: 10, color: Color(0xff333333)),
                      ),
                    ],
                    style: TextStyle(fontSize: 10, color: Color(0xff333333)),
                  )),
            )
          ],
        ));
  }

  Widget lotteryResultMyItem(My bean) {
    // return LotteryResultListItem('${widget.}');
    String title = bean.type == 1
        ? "优惠券"
        : bean.type == 2
            ? "商品"
            : bean.type == 3
                ? "积分"
                : bean.type == 4
                    ? "红包"
                    : "";

    return bean.type == 1 || bean.type == 4 || bean.type == 3
        ? Container(
            height: 40,
            child: Row(
              children: [
                Expanded(
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                      Text(
                        '抽中${title}',
                        style:
                            TextStyle(fontSize: 12, color: Color(0xff333333)),
                      ),
                      Text(
                        '${bean.drawDate}',
                        style: TextStyle(fontSize: 8, color: Color(0xff666666)),
                      )
                    ])),
                SizedBox(
                  width: 50,
                ),
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      '${bean.goodsName}',
                      style: TextStyle(fontSize: 12, color: Color(0xff333333)),
                    ),
                    Text(
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      '已发放，请前往我的查看',
                      style: TextStyle(fontSize: 8, color: Color(0xff666666)),
                    )
                  ],
                ))
              ],
            ),
          )
        : Container(
            height: 40,
            child: Row(
              children: [
                Expanded(
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                      Text(
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        '抽中${title}',
                        style:
                            TextStyle(fontSize: 10, color: Color(0xff333333)),
                      ),
                      Text(
                        '${bean.drawDate}',
                        style: TextStyle(fontSize: 8, color: Color(0xff666666)),
                      )
                    ])),
                SizedBox(
                  width: 50,
                ),
                Expanded(
                    child: GestureDetector(
                  onTap: () {
                    if (bean.type != 2) {
                      return;
                    }
                    if (bean.received == 1) {
                      Tools.showToast('已领取');
                    } else if (bean.received == 2) {
                      Tools.showToast('已过期');
                    } else if (bean.received == 0) {
                      //跳转领取
                      Navigator.pop(context);
                      Map<String, dynamic> params = {};
                      //todo ------------>
                      params['type'] = 0;
                      params['client'] = GetPlatform.isIOS
                          ? AppConfig.clientIos
                          : AppConfig.clientAndroid;
                      params['goodsList'] = getPlaceOrderGoodsList(bean);
                      params['sid'] = AppData.sid;
                      params['isRaffleGoods'] = 1;
                      Get.toNamed(Routes.fillOrder, arguments: params);
                    }
                  },
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '${bean.goodsName}',
                        style: TextStyle(fontSize: 10),
                      ),
                      Text(
                        bean.type != 2
                            ? '已领取'
                            : bean.received == 1
                                ? "已领取"
                                : "未领取，点击领取",
                        style: bean.received == 1
                            ? TextStyle(fontSize: 8)
                            : TextStyle(fontSize: 8, color: Color(0xffff5141)),
                      )
                    ],
                  ),
                ))
              ],
            ),
          );
  }

  ///下单商品列表
  List<Map<String, dynamic>> getPlaceOrderGoodsList(My bean) {
    List<Map<String, dynamic>> arr = [];
    Map<String, dynamic> info = {};
    info['buyNum'] = 1;
    info['goodsId'] = bean.typeValue;
    info['isSelect'] = 1;
    info['isSend'] = 0;
    info['specNum'] = 1;
    arr.add(info);
    return arr;
  }
}
